﻿using ConfExpert.Forms.Aksesuar.Satinalma;
using ConfExpert.Forms.Base;
using ConfExpert.Forms.Depo.Aksesuar;
using DevExpress.XtraEditors;
using DevExpress.XtraGrid.Views.Grid;
using DevExpress.XtraReports.UI;
using DevTeam;
using System;
using System.Data;
using System.Drawing;
using System.Windows.Forms;

namespace ConfExpert.Forms.Depo
{
    public partial class AksesuarIhtiyaciPlanlama : XForm
    {
        public AksesuarIhtiyaciPlanlama()
        {
            InitializeComponent();
        }
        public AksesuarIhtiyaciPlanlama(string planlamaIds)
            : this()
        {
            PlanlamaIds = planlamaIds;
        }

        string PlanlamaIds;
        private void AksesuarIhtiyaci_Load(object sender, EventArgs e)
        {
            MainToolbar.Visible = true;
            gvTreeLines.Init("createAksesuarTalep", PlanlamaIds);

            gvTreeLines.AddColumn("EksikMiktar", typeof(int), "IIF(TalepEdilen-Isnull(AyrilanMiktar,0)-Isnull(QtyPO,0) > 0, TalepEdilen-Isnull(AyrilanMiktar,0)-Isnull(QtyPO,0),0)");
            gvTreeLines.MakeReadOnly("TalepEdilen");
            gvTreeLines.YanYanaGetir("EksikMiktar:AyrilanMiktar");
            gvTreeLines.MakeColumnInvisible("OrtakMi");

            gvTreeLines.FormatAsNumber(0, "TalepEdilen", "AyrilanMiktar", "IdleMiktar", "QtyPO", "ToplamStok", "EksikMiktar"/*, "DigerMarkaStok", "ToplamCikilan", "MusteridenGelecek"*/);
            gvTreeLines.AddSummary("TalepEdilen", "AyrilanMiktar", "IdleMiktar", "QtyPO", "ToplamStok", "EksikMiktar"/*, "DigerMarkaStok", "ToplamCikilan", "MusteridenGelecek"*/);

            gvTreeLines.BestFitColumns();
            gvTreeLines.RowCellStyle += new RowCellStyleEventHandler(gvTreeLines_RowCellStyle);

            bbiAksPO.ItemClick += bbiAksPO_ItemClick;
            bbiTalepOlustur.ItemClick += bbiTalepOlustur_ItemClick;
            bbiRezerve.ItemClick += bbiRezerve_ItemClick;
            gvTreeLines.CellValueChanged += gvTreeLines_CellValueChanged;

            SetButtonsEnabled();
        }

        void gvTreeLines_CellValueChanged(object sender, DevExpress.XtraGrid.Views.Base.CellValueChangedEventArgs e)
        {
            gvTreeLines.EndEditing();
            if (e.RowHandle > -1)
                if (e.Column.FieldName == "TalepEdilen")
                    SetButtonsEnabled();
        }

        void bbiRezerve_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            DataTable dtAksesuar = gvTreeLines.DataTable();
            DataRow[] dr = dtAksesuar.Select("Isnull(IdleMiktar,0) > 0 and Isnull(EksikMiktar,0) > 0 and Isnull(OrtakMi,0) = 0");

            if (dr.Length > 0)
            {
                DataTable dt = dr.CopyToDataTable();
                dt.TableName = "Aksesuar";
                dt.Columns["TalepEdilen"].ColumnName = "ToplamIhtiyac";
                dt.Columns.Add("QtySelected", typeof(int), "IIF( IsNull(IdleMiktar, 0) < IsNull(EksikMiktar, 0), IsNull(IdleMiktar, 0), IsNull(EksikMiktar, 0))");

                string xmlSel = dt.Serialize();//XML.SerializeAll(dt);

                using (AksesuarAyir transfer = new AksesuarAyir(xmlSel, 47, 3, ""))
                    if (transfer.ShowDialog() == DialogResult.OK)
                        Yenile();
            }
        }

        void bbiTalepOlustur_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            //DataTable dtAksesuar = gvTreeLines.DataTable();
            int EksikRowCount = gvTreeLines.DataTable().Select("Isnull(EksikMiktar,0) > 0").Length;
            if (EksikRowCount > 0)
                if (XtraMessageBox.Show("Eksik miktarlar, talep miktarından çıkarılacak. Devam etmek istiyor musunuz ?", "Critical", MessageBoxButtons.YesNo) == System.Windows.Forms.DialogResult.No)
                    return;
                else
                    foreach (DataRow dr in gvTreeLines.DataTable().Select("Isnull(EksikMiktar,0) > 0"))
                    {
                        dr["TalepEdilen"] = dr["TalepEdilen"].ToInt() - dr["EksikMiktar"].ToInt();
                    }
            DataTable dtAksesuar = gvTreeLines.DataTable();
            string xml = gvTreeLines.Serialize();
            string DosyaNo = gvTreeLines.Dataset().Tables["Table1"].Rows[0]["DosyaNo"].ToStr();

            using (AksesuarCikisTalebi ac = new AksesuarCikisTalebi(xml, DosyaNo, 47, 3, "", false, "",293))
                if (ac.ShowDialog() == DialogResult.OK)
                {
                    this.DialogResult = DialogResult.OK;
                }
        }

        void bbiAksPO_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            DataTable dtAksesuar = gvTreeLines.DataTable();
            DataRow[] drIdle = dtAksesuar.Select("Isnull(IdleMiktar,0) > 0 and Isnull(EksikMiktar,0) > 0 and Isnull(OrtakMi,0) = 0");

            if (drIdle.Length > 0)
            {
                if (XtraMessageBox.Show("Bu siparişe ayırabileceğiniz IDLE stok var. İlk önce IDLE stoğu siparişe ayırmak ister misiniz ?", "Critical", MessageBoxButtons.YesNo) == System.Windows.Forms.DialogResult.Yes)
                    bbiRezerve.PerformClick();
            }

            DataRow[] dr = dtAksesuar.Select("Isnull(EksikMiktar,0) > 0");

            if (dr.Length > 0)
            {
                DataTable dt = dr.CopyToDataTable();

                using (Talep ast = new Talep(dt, 47, 3, ""))
                    if (ast.ShowDialog() == DialogResult.OK)
                    {
                        string AksPOId = ast.POIds.ToString();
                        Rapor.Aksesuar.xrAksesuarSiparisFoy asf = new Rapor.Aksesuar.xrAksesuarSiparisFoy(AksPOId, "GetAksesuarSiparisFoyu", "AksPOId");
                        asf.ShowPreview();
                        Yenile();
                    }
            }
        }

        private void Yenile()
        {
            gvTreeLines.RefreshDataSource(true, "createAksesuarTalep", PlanlamaIds);
            SetButtonsEnabled();
        }

        private void SetButtonsEnabled()
        {
            DataTable dtAksesuar = gvTreeLines.DataTable();

            int RzvRowCount = dtAksesuar.Select("Isnull(IdleMiktar,0) > 0 and Isnull(EksikMiktar,0) > 0 and Isnull(OrtakMi,0) = 0").Length;
            int EksikRowCount = dtAksesuar.Select("Isnull(EksikMiktar,0) > 0").Length;
            bool IsEnoughQty = dtAksesuar.Select("(Isnull(EksikMiktar,0) > TalepEdilen * 0.05 and Isnull(EksikMiktar,0) > 2) or Isnull(EksikMiktar,0) = TalepEdilen").Length == 0;

            bbiAksPO.Enabled = EksikRowCount > 0;
            bbiRezerve.Enabled = RzvRowCount > 0;

            bbiTalepOlustur.Enabled = IsEnoughQty;
        }

        void gvTreeLines_RowCellStyle(object sender, RowCellStyleEventArgs e)
        {
            GridView view = sender as GridView;
            if (e.RowHandle > -1)
            {
                int TalepEdilen = view.GetRowCellValue(e.RowHandle, "TalepEdilen").ToInt();
                int EksikMiktar = view.GetRowCellValue(e.RowHandle, "EksikMiktar").ToInt();

                if (e.Column.FieldName == "TalepEdilen")
                {
                    int AyrilanMiktar = view.GetRowCellValue(e.RowHandle, "AyrilanMiktar").ToInt();
                    int IdleMiktar = view.GetRowCellValue(e.RowHandle, "IdleMiktar").ToInt();

                    int BrandId = view.GetRowCellValue(e.RowHandle, "BrandId").ToInt();

                    if ((AyrilanMiktar < TalepEdilen && BrandId != 1) || (IdleMiktar < TalepEdilen && BrandId == 1))
                        e.Appearance.BackColor = Color.Pink;
                    e.Appearance.Font = new Font(DevExpress.Utils.AppearanceObject.DefaultFont, FontStyle.Bold);
                }

                if (e.Column.FieldName == "EksikMiktar")
                {
                    e.Appearance.Font = new Font(DevExpress.Utils.AppearanceObject.DefaultFont, FontStyle.Bold);
                    e.Appearance.ForeColor = (EksikMiktar > TalepEdilen * 0.05 && EksikMiktar > 2) || TalepEdilen == EksikMiktar ? Color.Red : Color.Green;
                }

            }
        }
    }
}